<?php

/**
 * 是否JSON 字符串
 * @param $string
 * @return bool|mixed
 */
function is_json($string)
{
	$data = json_decode($string, true);

	if (JSON_ERROR_NONE == json_last_error()) {
		return $data;
	}
	else {
		return false;
	}
}

/**
 * 是否XML 字符串
 * @param $string
 * @return bool|mixed
 */
function is_xml($string)
{
	$xml = @simplexml_load_string(trim($string));
	if (!$xml) {
		return false;
	}
	else {
		return json_decode(json_encode($xml), true);
	}
}

/**
 * 清除定时任务
 * @param $timerId
 */
function clearInterval($timerId)
{
	swoole_timer_clear($timerId);
}


/**
 * 获取文件后缀
 * @param $fileName
 * @return mixed
 */
function fileSuffix($fileName)
{
	$name = explode('.', $fileName);
	return @$name[count($name) - 1];
}

/**
 * 服务器退出
 */
function shutdown()
{
	@\lib\Server\Server::$SwooleInstance->shutdown();
	exit(255);
}

/**
 * 服务器重启
 */
function reload()
{
	\lib\Server\Server::$SwooleInstance->reload();
}

/**
 * 服务器停止
 */
function stop()
{
	\lib\Server\Server::$SwooleInstance->stop();
}

/**
 * 人民币 分转元
 * @param $price
 * @return mixed
 */
function to_yuan($price)
{
	return str_replace(',', '', number_format($price / 100, 2));
}

/**
 * 人民币 元转分
 * @param $price
 * @return int
 */
function to_fen($price)
{
	return (int)($price * 100);
}

/**
 * 获取指定key 生成新索引数组
 * @param $data
 * @param $key
 * @return array
 */
function array_to_new_array_key($data, $key)
{
	$new_array = [];

	foreach ($data as $k => $val) {
		$new_array[][$key] = $val[$key];
	}

	return $new_array;
}

/**
 * 创建临时文件
 * @param $dir
 * @param $name
 * @param $context
 * @return bool|string
 */
function create_tmp_file($dir, $name, $context)
{
	if (!is_dir($dir)) {
		mkdir($dir, 0777, true);
	}
	$file_path_name = $dir . DIRECTORY_SEPARATOR . uniqid() . "-{$name}";
	if (file_put_contents($file_path_name, $context)) {
		return $file_path_name;
	}
	else {
		return false;
	}
}

/**
 * 删除目录
 * @param $dir
 * @return bool
 */
function delete_dir($dir)
{
	if (!is_dir($dir)) {
		return false;
	}

	$dh = opendir($dir);
	while ($file = readdir($dh)) {
		if ($file != "." && $file != "..") {
			$fullpath = $dir . "/" . $file;
			if (!is_dir($fullpath)) {
				unlink($fullpath);
			}
			else {
				delete_dir($fullpath);
			}
		}
	}
	closedir($dh);
	if (rmdir($dir)) {
		return true;
	}
	else {
		return false;
	}
}

/**
 * 安全移动上传文件
 * @param $upload_tmp
 * @param $dir
 * @param bool $safe
 * @return bool
 */
function move_upload_file($upload_tmp, $dir, $safe = true)
{
	if ($safe) {
		if (is_uploaded_file($upload_tmp)) {
			return move_uploaded_file($upload_tmp, $dir);
		}
		else {
			$_ = explode(DIRECTORY_SEPARATOR, $upload_tmp);
			if ($_[count($_) - 2] != \lib\Server\Service\TcpHttpService::$session_id) {
				return false;
			}
			else {
				return copy($upload_tmp, $dir);
			}
		}
	}

	return copy($upload_tmp, $dir);
}


/**
 * 随机字符串
 * @param int $len
 * @return string
 */
function nonce_str($len = 32)
{
	$str_arr     = array_merge(range(0, 10), range('a', 'z'), range('A', 'Z'));
	$str_arr_max = count($str_arr) - 1;
	for ($i = 0; $i < $len; $i++) {
		$nonce_str[] = $str_arr[mt_rand(0, $str_arr_max)];
	}

	return join('', $nonce_str);
}

/**
 * 数组转xml
 * @param array $arr
 * @return string
 */
function array_to_xml($arr)
{
	$xml = "<xml>";
	foreach ($arr as $key => $val) {
//		if (is_numeric($val)) {
		$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
//		}
//		else {
//			$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
//		}
	}
	$xml .= "</xml>";
	return $xml;
}


function xml_to_array($xml_string)
{
	return json_decode(json_encode((array)simplexml_load_string($xml_string, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
